package com.houhanzhi.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.houhanzhi.manager.TrafficManager;
import com.houhanzhi.mapper.TrafficMapper;
import com.houhanzhi.model.TrafficDO;
import com.houhanzhi.util.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
@Slf4j
public class TrafficManagerImpl implements TrafficManager {

    @Autowired
    private TrafficMapper trafficMapper;

    /**
     * 新增流量包
     *
     * @param trafficDO
     * @return
     */
    @Override
    public int add(TrafficDO trafficDO) {
        return trafficMapper.insert(trafficDO);
    }

    /**
     * 分页查询可用的流量包
     *
     * @param pageInfo
     * @param accountNo
     * @return
     */
    @Override
    public IPage<TrafficDO> pageAvailable(Page<TrafficDO> pageInfo, long accountNo) {

        String today = TimeUtil.format(new Date(), "yyyy-MM-dd");
        return trafficMapper.selectPage(pageInfo, new QueryWrapper<TrafficDO>()
                .eq("account_no", accountNo)
                .ge("expired_date", today)
                .orderByDesc("gmt_create")
        );
    }


    /**
     * 查找流量包详情
     *
     * @param trafficId
     * @param accountNo
     * @return
     */
    @Override
    public TrafficDO findByIdAndAccountNo(Long trafficId, long accountNo) {
        return trafficMapper.selectOne(new QueryWrapper<TrafficDO>()
                .eq("id", trafficId)
                .eq("account_no", accountNo));
    }

    /**
     * 删除过期的流量包(付费)
     *
     * @return
     */
    @Override
    public boolean deleteExpiredTraffic() {
        final int rows = trafficMapper.delete(new QueryWrapper<TrafficDO>().le("expired_date", new Date()));
        log.info("删除过期流量包行数:rows={}", rows);
        return true;
    }

    /**
     * 查找可用的短链流量包(未过期,不一定可用，有的会超过可用次数),包括免费流量包
     *
     * @param accountNo
     * @return
     */
    @Override
    public List<TrafficDO> selectAvailableTraffics(Long accountNo) {
        String today = TimeUtil.format(new Date(),"yyyy-MM-dd");
        //SELECT  * FROM traffic_0 WHERE (account_no = ? AND (expired_date >= ? OR out_trade_no = ?))
        QueryWrapper<TrafficDO> queryWrapper = new QueryWrapper<TrafficDO>();
        queryWrapper.eq("account_no", accountNo);
        // 收费+免费
        queryWrapper.and(wrapper -> wrapper.ge("expired_date", today).or().eq("out_trade_no","free_init"));

        List<TrafficDO> trafficDOList = trafficMapper.selectList(queryWrapper);

        return trafficDOList;
    }

    /**
     * 给某个流量包增加使用次数
     *
     * @param accountNo
     * @param trafficId
     * @param usedTimes
     * @return
     */
    @Override
    public int addDayUsedTimes(Long accountNo, Long trafficId, Integer usedTimes) {
        return trafficMapper.addDayUsedTimes(accountNo, trafficId, usedTimes);
    }

    /**
     * 恢复流量包使用当天次数
     * @param accountNo
     * @param trafficId
     * @param useTimes
     * @param useDateStr
     */
    @Override
    public int releaseUsedTimes(long accountNo, Long trafficId, Integer useTimes, String useDateStr) {
        return trafficMapper.releaseUsedTimes(accountNo, trafficId, useTimes, useDateStr);
    }

    /**
     * 批量更新流量包使用次数为0
     * @param accountNo
     * @param unUpdatedTrafficIds
     */
    @Override
    public int batchUpdateUsedTimes(Long accountNo, List<Long> unUpdatedTrafficIds) {
        return trafficMapper.update(null, new UpdateWrapper<TrafficDO>()
                .eq("account_no", accountNo)
                .in("id", unUpdatedTrafficIds)
                .set("day_used", 0)
        );
    }
}
